const db = require("../db/index");
// 获取
exports.getStu = (req, res) => {
  const sql = `select * from ev_stu where is_delete=0 order by id asc `;
  db.query(sql, (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err);

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: "获取学生列表成功！",
      data: results,
      total: results.length,
    });
  });
};
// 添加
exports.addStu = (req, res) => {
  const stuInfo = {
    ...req.body,
    date: new Date(),
  };
  const sql = `insert into ev_stu set ?`;
  db.query(sql, stuInfo, (err, results) => {
    if (err) return res.cc(err);
    res.send({ status: 0, message: "新增学生数据成功!" });
  });
};
// 删除
exports.delStu = (req, res) => {
  const sql = `update ev_stu set is_delete = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除学生数据失败!");
    res.send({ status: 0, message: "删除学生数据成功!" });
  });
};
// 更新
exports.updateStu = (req, res) => {
  const sql = `update ev_stu set ? where id=?`;
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("更新学生数据失败!");
    res.send({ status: 0, message: "更新学生数据成功!" });
  });
};
